﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ole = AnalysisSystem.OleDbMethod;

namespace AnalysisSystem
{
    class DailyProductionSummary
    {
        public static DataTable dailyProduction = new DataTable();
        /// <summary>
        /// 计算日产汇总
        /// </summary>
        public static void DailyProductionSum()
        {
            InitialTable();
            int startRow = 0, endRow = -1;
            for (int i = 0; i < dailyProduction.Rows.Count; i++)
            {
                startRow = endRow + 1;
                DateTime date = Convert.ToDateTime(dailyProduction.Rows[i][1]);
                //顺序为套压、油压
                int[] productionWell = new int[2];
                double[] sumData = new double[2];
                double[] averageData = new double[2];
                //顺序为油、水、气
                double[] dailyData = new double[3];
                //计算套压、油压均值，记录油，水，气日产值
                for (int j = startRow; DateCheck(date, j); j++)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        dailyData[k] += ValueProcess(j, 6 + k);
                    }
                    for (int k = 0; k < 2; k++)
                    {
                        double value = ValueProcess(j, 4 + k);
                        sumData[k] += value;
                        if (!value.Equals(0))
                            productionWell[k]++;
                    }
                    endRow = j;
                }
                //计算油，水，气日产均值
                for (int k = 0; k < 2; k++)
                {
                    if (productionWell[k].Equals(0))
                        averageData[k] = 0;
                    else
                        averageData[k] = sumData[k] / productionWell[k];
                    //else
                    //    averageData[k] = sumData[k] / (endRow - startRow + 1);
                }
                //填充数据,当日无数据则不填充
                if (endRow < startRow)
                {
                    continue;
                }
                FillOtherData(i, startRow, endRow);
                FillProductionData(i, dailyData, averageData);
            }
        }
        #region 私有函数
        /// <summary>
        /// 初始化建表
        /// </summary>
        private static void InitialTable()
        {
            string[] colName = { "序号", "日期", "起始行", "终止行", "生产井数（口）",
                                 "平均套压（Mpa）", "平均油压（Mpa）",
                                 "日产油（吨）", "日产水（方）", "日产气（万方）" };
            dailyProduction.TableName = "4-日产汇总";
            int lastIndex = Ole.dataTables[2].Rows.Count - 1;
            DateTime startDate = Convert.ToDateTime(Ole.dataTables[2].Rows[0][3]);
            DateTime endDate = Convert.ToDateTime(Ole.dataTables[2].Rows[lastIndex][3]);
            endDate = endDate.AddDays(1);
            //根据colName添加列名
            for (int i = 0; i < colName.Length; i++)
            {
                dailyProduction.Columns.Add(colName[i]);
            }
            //将序号及日期填入
            int number = 0;
            for (DateTime start = startDate; !start.Equals(endDate); start = start.AddDays(1), number++)
            {
                dailyProduction.Rows.Add();
                dailyProduction.Rows[number][0] = number + 1;
                dailyProduction.Rows[number][1] = start.ToShortDateString();
            }
        }
        /// <summary>
        /// 日期比较
        /// </summary>
        /// <param name="date">日期</param>
        /// <param name="index">行数</param>
        /// <returns></returns>
        private static bool DateCheck(DateTime date, int index)
        {
            if (index >= Ole.dataTables[2].Rows.Count) return false;
            DateTime dateCompare = Convert.ToDateTime(Ole.dataTables[2].Rows[index][3]);
            if (date.Equals(dateCompare)) return true;
            else return false;
        }
        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="row">行</param>
        /// <param name="col">列</param>
        /// <returns></returns>
        private static double ValueProcess(int row, int col)
        {
            double value;
            if (Ole.dataTables[2].Rows[row][col].Equals(DBNull.Value))
                value = 0;
            else
                value = Convert.ToDouble(Ole.dataTables[2].Rows[row][col]);
            return value;
        }
        /// <summary>
        /// 填充其它数据
        /// </summary>
        /// <param name="index"></param>
        /// <param name="startRow"></param>
        /// <param name="endRow"></param>
        private static void FillOtherData(int index, int startRow, int endRow)
        {
            dailyProduction.Rows[index][2] = startRow;
            dailyProduction.Rows[index][3] = endRow;
            dailyProduction.Rows[index][4] = endRow - startRow + 1;
        }
        /// <summary>
        /// 填充数值数据
        /// </summary>
        /// <param name="index">行</param>
        /// <param name="dailyData">日产数据</param>
        /// <param name="averageData">平均数据</param>
        private static void FillProductionData(int index, double[] dailyData, double[] averageData)
        {
            for (int i = 0; i < 2; i++)
            {
                dailyProduction.Rows[index][5 + i] = Math.Round(averageData[i], 4);
            }
            for (int i = 0; i < 3; i++)
            {
                dailyProduction.Rows[index][7 + i] = Math.Round(dailyData[i], 4);
            }
        }
        #endregion
    }
}
